/*
	-- 基于jQuery(1.7)扩展插件 Slider --
author : kongge
create : 2011.12.13
update : 2012.03.28
-- 接口说明 --
	- @Param options {Object} #2011.12.05#
		- {
			length : 3,
			shell : null,
			auto : false,
			cName : 'left',
			duration : 200,
			unitSize : 500,
			delay : 5000,
			onbeforeplay : _noop,	//此时如果return false; 将会停止此次动画
			onplay : _noop,
			ondone : _noop
		}
-- 接口说明 end --
*/
;(function(){
	var 
	_noop = function(){},
	slider = 
	window.Slider = function(ops){
		this.init(ops || {});
	};
	slider.prototype = {
		constructor : slider,
		_ops : {
			length : 4,
			shell : null,
			auto : false,
			autoStop : true,
			cName : 'left',
			duration : 200,
			unitSize : 500,
			delay : 5000,
			easing : 'swing',
			onplay : _noop,
			ondone : _noop,
			onbeforeplay : _noop
		},
		init : function(ops){
			var 
			k,
			self = this,
			_ops = this._ops;
			for(k in _ops){
				if(typeof ops[k] === 'undefined'){
					ops[k] = _ops[k];
				}
			}
			this.index = 0;
			this.ops = ops;
			if(ops.auto){
				this.autoPlay();
				ops.autoStop && ops.shell.hover(function(){
					self.stopAuto();
				}, function(){
					self.autoPlay();
				});
			}
		},
		play : function(inx){
			inx = parseInt(inx, 10);
			inx = isFinite(inx) ? inx : this.index + 1;
			var 
			ani = {},
			self = this,
			ops = this.ops,
			shell = ops.shell,
			len = ops.length;
			inx = (inx < 0 ? len - 1 : inx) % len;
			if(!shell || !shell.animate || inx === this.index || ops.onbeforeplay.call(this, inx, this.index) === false) return this;
			ani[ops.cName] = -inx * ops.unitSize;
			shell.stop().animate(ani, ops.duration, ops.easing, function(){
				ops.ondone.call(self, inx, self.index);
				self.index = inx;
			});
			ops.onplay.call(self, inx, this.index);
			return this;
		},
		next : function(){
			return this.play();
		},
		prev : function(){
			return this.play(this.index - 1);
		},
		autoPlay : function(){
			var 
			self = this;
			this._timer = setTimeout(function(){
				self.play().autoPlay();
			}, this.ops.delay);
		},
		stopAuto : function(){
			window.clearTimeout(this._timer);
		}
	};
})();